package com.warm.androidstudy.utils;

import android.text.TextUtils;
import android.util.Pair;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 *
 * @author Administrator
 * @date 2017/6/28 0028
 */

public class StringUtils {

    private StringUtils() {
        throw new AssertionError();
    }

    public static boolean isEmpty(CharSequence str) {
        return TextUtils.isEmpty(str);
    }

    public static boolean isEmptyWithNullStr(CharSequence str) {
        return isEmpty(str) || str.toString().toLowerCase().equals("null");
    }

    public static String processNullStr(String str) {
        return TextUtils.isEmpty(str) ? "" : (!str.equals("null") && !str.equals("nul") ? str : "");
    }

    public static boolean containsUseLoop(String[] arr, String targetValue) {
        String[] var2 = arr;
        int var3 = arr.length;

        for (int var4 = 0; var4 < var3; ++var4) {
            String s = var2[var4];
            if (s.equals(targetValue)) {
                return true;
            }
        }

        return false;
    }

    public static String processNullTarget(String str, String target) {
        return isEmptyWithNullStr(str) ? target : str;
    }

    public static int length(CharSequence str) {
        return str == null ? 0 : str.length();
    }

    public static String capitalizeFirstLetter(String str) {
        if (isEmpty(str)) {
            return str;
        } else {
            char c = str.charAt(0);
            return Character.isLetter(c) && !Character.isUpperCase(c) ? (new StringBuilder(str.length())).append(Character.toUpperCase(c)).append(str.substring(1)).toString() : str;
        }
    }

    public static String utf8Encode(String str, String defultReturn) {
        if (!isEmpty(str) && str.getBytes().length != str.length()) {
            try {
                return URLEncoder.encode(str, "UTF-8");
            } catch (UnsupportedEncodingException var3) {
                return defultReturn;
            }
        } else {
            return str;
        }
    }


    public static String htmlEscapeCharsToString(String source) {
        return isEmpty(source) ? source : source.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&").replaceAll("&quot;", "\"");
    }

    public static String fullWidthToHalfWidth(String s) {
        if (isEmpty(s)) {
            return s;
        } else {
            char[] source = s.toCharArray();

            for (int i = 0; i < source.length; ++i) {
                if (source[i] == 12288) {
                    source[i] = 32;
                } else if (source[i] >= '！' && source[i] <= '～') {
                    source[i] -= 'ﻠ';
                } else {
                    source[i] = source[i];
                }
            }

            return new String(source);
        }
    }

    public static String halfWidthToFullWidth(String s) {
        if (isEmpty(s)) {
            return s;
        } else {
            char[] source = s.toCharArray();

            for (int i = 0; i < source.length; ++i) {
                if (source[i] == 32) {
                    source[i] = 12288;
                } else if (source[i] >= 33 && source[i] <= 126) {
                    source[i] += 'ﻠ';
                } else {
                    source[i] = source[i];
                }
            }

            return new String(source);
        }
    }

    public static boolean containsEmoji(String source) {
        int len = source.length();

        for (int i = 0; i < len; ++i) {
            char codePoint = source.charAt(i);
            if (!isEmojiCharacter(codePoint)) {
                return true;
            }
        }

        return false;
    }

    private static boolean isEmojiCharacter(char codePoint) {
        return codePoint == 0 || codePoint == 9 || codePoint == 10 || codePoint == 13 || codePoint >= 32 && codePoint <= '\ud7ff' || codePoint >= '\ue000' && codePoint <= '�' || codePoint >= 65536 && codePoint <= 1114111;
    }


    public static boolean isEmail(String str){
        //电子邮件
        String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(str);
        boolean isMatched = matcher.matches();
        return isMatched;
    }

    public static boolean checkPws(String pwd){
        if (pwd.length()< 6 || pwd.length()>12){
            return false;
        }
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$";

        return pwd.matches(regex);
    }

    /**
     * 大陆号码或香港号码均可
     */
    public static boolean isPhoneLegal(String str) throws PatternSyntaxException {
        return isChinaPhoneLegal(str) || isHKPhoneLegal(str);
    }

    /**
     * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数
     * 此方法中前三位格式有：
     * 13+任意数
     * 145,147,149
     * 15+除4的任意数(不要写^4，这样的话字母也会被认为是正确的)
     * 166
     * 17+3,5,6,7,8
     * 18+任意数
     * 198,199
     */
    public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {
        // ^ 匹配输入字符串开始的位置
        // \d 匹配一个或多个数字，其中 \ 要转义，所以是 \\d
        // $ 匹配输入字符串结尾的位置
        String regExp = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" +
                "|(18[0-9])|(19[8,9]))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 香港手机号码8位数，5|6|8|9开头+7位任意数
     */
    public static boolean isHKPhoneLegal(String str) throws PatternSyntaxException {
        // ^ 匹配输入字符串开始的位置
        // \d 匹配一个或多个数字，其中 \ 要转义，所以是 \\d
        // $ 匹配输入字符串结尾的位置
        String regExp = "^(5|6|8|9)\\d{7}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 切割字符串
     * @param str
     * @param k 子节
     * @return
     */
    public static List<Pair<String, String>> subString(String str, int k){
        List<Pair<String, String>> texts = new ArrayList<>();
        String res = "";
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            String tmpStr = str.substring(i,i+1);
            res += tmpStr;
            count += tmpStr.getBytes().length;
            if(count > k){
                texts.add(new Pair<>(res,System.currentTimeMillis()+""));
                res = "";
                count = 0;
            }
            if(i == str.length()-1 && count < k){
                texts.add(new Pair<>(res,System.currentTimeMillis()+""));
            }
        }
        return texts;
    }

  public static String filterVoiceStr(String str){

        if (!str.contains("#")){
            return str;
        }

        int index = str.indexOf("#");

        return str.substring(0,index);
  }



}

